/** @file    wCore_Tree.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wCore_Tree.cpp 2618 2010-06-04 09:48:15Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wCore_Tree class.
 * The header for this class can be found in wCore_Tree.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this tree of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this tree of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wCore_Tree.h"

 #include <core/ObjectAllocator.h>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::wCore_Tree, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Tree )
 WOSH_GUI_MAP(wosh::gui::wCore_Tree, "wosh::Tree", 0.0, _static_Map_wCore_Tree )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

wCore_Tree::wCore_Tree( QWidget* parent )
	: QTreeView(parent), treeModel(NULL) {

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	this->treeModel = new TreeModel_Core_Tree(this);
	QTreeView::setModel(this->treeModel);
//setSortingEnabled(true);

 }

wCore_Tree::~wCore_Tree() {
	if ( this->treeModel != NULL )
		delete this->treeModel; this->treeModel = NULL;
 }


WRESULT wCore_Tree::loadObject( const wosh::Tree* object ) {
	return this->treeModel->loadObject(object);
 }

WRESULT wCore_Tree::saveObject( wosh::Tree* object ) const {
	if ( object == NULL ) return WRET_ERR_PARAM;

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

TreeModel_Core_Tree::TreeModel_Core_Tree(QObject *parent)
	: QAbstractItemModel(parent), dataTree(NULL) {

 }

TreeModel_Core_Tree::~TreeModel_Core_Tree() {
	if ( this->dataTree != NULL )
		delete this->dataTree; this->dataTree = NULL;
 }

WRESULT TreeModel_Core_Tree::loadObject( const wosh::Tree* tree ) {
	if ( this->dataTree != NULL )
		delete this->dataTree; this->dataTree = NULL;
	if ( tree == NULL )
		return WRET_OK;
	this->dataTree = tree->clone();
	reset();
	return WRET_OK;
 }

WRESULT TreeModel_Core_Tree::acquireObject( wosh::Tree* tree ) {
	if ( this->dataTree != NULL )
		delete this->dataTree; this->dataTree = NULL;
	this->dataTree = tree;
	reset();
	return WRET_OK;
 }


int TreeModel_Core_Tree::rowCount( const QModelIndex &parent ) const {
	TreeNode *parentItem = NULL;
	if (!parent.isValid())
		parentItem = getRootNode();
	else
		parentItem = static_cast<TreeNode*>(parent.internalPointer());
	if ( parentItem == NULL )
		return 0;
	return parentItem->getChildren().size();
 }

QModelIndex TreeModel_Core_Tree::index( int row, int column, const QModelIndex &parent ) const {
	if (!hasIndex(row, column, parent))
		return QModelIndex();
	TreeNode* parentItem = NULL;
	if (!parent.isValid())
		parentItem = getRootNode();
	else
		parentItem = static_cast<TreeNode*>(parent.internalPointer());
	if ( parentItem == NULL )
		return QModelIndex();

	TreeNode* childItem = reinterpret_cast<TreeNode*>(parentItem->getChildren().itemAt(row));
	if (childItem)
		return createIndex(row, column, childItem);
	else
		return QModelIndex();
 }

QModelIndex TreeModel_Core_Tree::parent( const QModelIndex &child ) const {
	 if ( !child.isValid() )
		 return QModelIndex();
	TreeNode* childItem = static_cast<TreeNode*>(child.internalPointer());
	if ( childItem == NULL )
		return QModelIndex();

	TreeNode* parentItem = reinterpret_cast<TreeNode*>(childItem->getParent());
	if ( parentItem == getRootNode() )
		return QModelIndex();
	if ( parentItem == NULL )
		return QModelIndex();
	TreeNode* parentParentItem = reinterpret_cast<TreeNode*>(parentItem->getParent());
	if ( parentParentItem == NULL )
		return createIndex(1,0, parentItem);
	int childIndex = parentParentItem->getChildren().indexOf(parentItem);
	if( childIndex < 0 ) childIndex = 0;
	return createIndex(childIndex,0, parentItem);
 }

Qt::ItemFlags TreeModel_Core_Tree::flags( const QModelIndex &index ) const {
	if ( !index.isValid() ) return 0;
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

QVariant TreeModel_Core_Tree::data( const QModelIndex &index, int role ) const {
	if ( !index.isValid() )	return QVariant();
	TreeNode* item = static_cast<TreeNode*>(index.internalPointer());
	if ( item == NULL || item->getContent() == NULL ) return QVariant("NULL");
	if ( role == Qt::DisplayRole || role == Qt::EditRole ) { // cos completer!
		switch (index.column()) {
			case 0: return QVariant(item->getContent()->toString().c_str());
			default:
				return QVariant( item->getContent()->toString().c_str() );
		 }
	 }
	else if ( role == Qt::ToolTipRole ) {
		return QVariant( item->getContent()->getClassName() );
	 }
	else if ( index.column() == 0 && role == Qt::DecorationRole ) {
		return QVariant( GuiUtilities::createIconFor(item->getContent()->getClassName()) );
	 }
	return QVariant();
 }

QVariant TreeModel_Core_Tree::headerData( int section, Qt::Orientation orientation, int role ) const {
	if (role != Qt::DisplayRole) return QVariant();
	if (orientation == Qt::Horizontal) {
		 switch(section) {
			case 0:		return QString("Object");
			default:	return QVariant();
		  }
	 }
	return QVariant();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


 }; // namespace gui
}; // namespace wosh
